<link rel="stylesheet" href="/plugins/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css">
<div class="row-fluid" ng-controller="roleCtrl">
    <!--主体内容-->
    <div class="am-cf am-padding am-padding-bottom-0">
        <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">系统管理</strong> / <small>用户</small></div>
    </div>
    <hr>

    <div class="am-g">
        <div class="am-u-sm-6">
            <div class="am-panel am-panel-default">
                <div class="am-panel-hd">
                    <h3 class="am-panel-title">用户列表
                        <span class="am-badge am-badge-secondary am-margin-right" ng-bind="list.length"></span>
                        <a data-am-modal="{target: '#addAlert'}" data-toggle="modal" class="am-btn am-btn-success  am-btn-xs am-round am-fr">创建</a>
                    </h3>
                </div>
                <div class="am-panel-bd">
                    <table class="am-table">
                        <thead>
                        <tr>
                            <th>序号</th>
                            <th>用户名称</th>
                            <th>账号</th>
                            <th>密码</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr ng-repeat="item in list">
                            <td ng-bind="$index+1">1</td>
                            <td ng-bind="item.name">Mark</td>
                            <td ng-bind="item.account">Mark</td>
                            <td ng-bind="item.password">Mark</td>
                            <td>
                                <div class="am-btn-toolbar">
                                    <div class="am-btn-group am-btn-group-xs">
                                        <button class="am-btn am-btn-default am-btn-xs am-text-secondary" ng-click="selectCurIndex($index)"> 角色设置</button>
                                        <button class="am-btn am-btn-default am-btn-xs am-hide-sm-only" data-am-modal="{target: '#updateAlert'}" ng-click="updateUser($index)"> 修改</button>
                                        <button class="am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only"  ng-click="deleteUser($index)"> 删除</button>
                                    </div>
                                </div>

                                <!--<a class="btn btn-primary btn-mini" ng-click="selectCurIndex($index)">角色设置</a>-->
                                <!--<a data-am-modal="{target: '#updateAlert'}"  data-toggle="modal" class="btn btn-success btn-mini" ng-click="updateUser($index)">修改</a>-->
                                <!--<a class="btn btn-danger btn-mini" ng-click="deleteUser($index)">删除</a>-->
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>

            </div>
        </div>
        <div class="am-u-sm-6" ng-show="curIndex!=null">
            <div class="am-panel am-panel-default">
                <div class="am-panel-hd">
                    <h3 class="am-panel-title">
                        角色权限 【<span ng-bind="list[curIndex].name"></span>】

                    </h3>
                </div>
                <div class="am-panel-bd">
                    <ul id="treeDemo" class="ztree"></ul>
                    <button ng-click="treeSubmit()">保存</button>
                </div>

            </div>
        </div>
    </div>

    <div class="am-modal am-modal-confirm" tabindex="-1" id="updateAlert">
        <div class="am-modal-dialog">
            <div class="am-modal-hd">修改用户</div>
            <div class="am-modal-bd">
                <form class="am-form am-form-horizontal" enctype="multipart/form-data" id="updateForm">
                    <div class="am-form-group">
                        <label for="user-name" class="am-u-sm-3 am-form-label">用户名称</label>
                        <div class="am-u-sm-9">
                            <input type="text" name="name" type="text" ng-model="list[curIndex].name" placeholder="请输入用户名称">
                        </div>
                    </div>
                    <div class="am-form-group">
                        <label for="user-name" class="am-u-sm-3 am-form-label">账号</label>
                        <div class="am-u-sm-9">
                            <input type="text" name="account" type="text" ng-model="list[curIndex].account" placeholder="请输入账号">
                        </div>
                    </div>
                    <div class="am-form-group">
                        <label for="user-name" class="am-u-sm-3 am-form-label">密码</label>
                        <div class="am-u-sm-9">
                            <input type="text" name="password" type="text" ng-model="list[curIndex].password" placeholder="请输入密码">
                        </div>
                    </div>
                </form>
            </div>
            <div class="am-modal-footer">
                <span class="am-modal-btn" data-am-modal-cancel>取消</span>
                <span class="am-modal-btn" data-am-modal-confirm ng-click="updateUserSubmit()">确定</span>
            </div>
        </div>
    </div>


    <div class="am-modal am-modal-confirm" tabindex="-1" id="addAlert">
        <div class="am-modal-dialog">
            <div class="am-modal-hd">创建用户</div>
            <div class="am-modal-bd">
                <form class="am-form am-form-horizontal" enctype="multipart/form-data" id="addForm">
                    <div class="am-form-group">
                        <label for="user-name" class="am-u-sm-3 am-form-label">用户名称</label>
                        <div class="am-u-sm-9">
                            <input type="text" name="name" type="text"  placeholder="请输入用户名称">
                        </div>
                    </div>
                    <div class="am-form-group">
                        <label for="user-name" class="am-u-sm-3 am-form-label">账号</label>
                        <div class="am-u-sm-9">
                            <input type="text" name="account" type="text" placeholder="请输入账号">
                        </div>
                    </div>
                    <div class="am-form-group">
                        <label for="user-name" class="am-u-sm-3 am-form-label">密码</label>
                        <div class="am-u-sm-9">
                            <input type="text" name="password" type="text" placeholder="请输入密码">
                        </div>
                    </div>
                </form>
            </div>
            <div class="am-modal-footer">
                <span class="am-modal-btn" data-am-modal-cancel>取消</span>
                <span class="am-modal-btn" data-am-modal-confirm ng-click="addUserSubmit()">确定</span>
            </div>
        </div>
    </div>



</div>



<SCRIPT type="text/javascript">
    function roleCtrl($scope,$http){

        //获取用户列表
        angularHttpPost($http,"/api/acl_role/getList",{},function(data){
            console.log("role/getList:",data);
            $scope.roleList = data.result;

            $.fn.zTree.init($("#treeDemo"), setting,$scope.roleList );
        });


        //获取用户列表
        angularHttpPost($http,"/api/acl_user/getList",{},function(data){
            console.log("userList:",data.result);
            $scope.list = data.result;
        });

        $scope.selectCurIndex = function(index){
            console.log("cur index:",index);
            $scope.curIndex = index;
            var curUser = $scope.list[index];
            console.log("curUser:",curUser.id);
//            获取用户的权限列表
            angularHttpPost($http,"/api/acl_user/userRoles",{userId:curUser.id},function(data){
                console.log("userRoles:",data.result);
                var zTree = $.fn.zTree.getZTreeObj("treeDemo");
                zTree.checkAllNodes(false);
                if(data.result){  //若权限不为空
                    data.result.forEach(function(item){
                        var nodes = zTree.getNodesByParam("id", item, null);
                        console.log(item,"nodes:",nodes);
                        if(nodes[0]){  //若node存在
                            zTree.checkNode(nodes[0], true, false);
                        }
                    });
                }

            });
        };
        $scope.addRole = function(){

        }

        $scope.updateUser = function(index){
            $scope.curIndex = index;
        }

        $scope.updateUserSubmit = function(){
            var obj = $scope.list[$scope.curIndex];
            var data = new FormData($('#updateForm')[0]);
            data.append('id', obj.id );
            ajaxFormPost("/api/acl_user/update",data,function(data){
                angularHttpPost($http,"/api/acl_user/getList",{},function(data){
//                    console.log(data);
                    $scope.list = data.result;
                });
                alert(data.code+":"+data.msg);

            });
        }

        $scope.addUserSubmit = function(){
            var data = new FormData($('#addForm')[0]);
            ajaxFormPost("/api/acl_user/add",data,function(data){
                console.log("data:",data);
                angularHttpPost($http,"/api/acl_user/getList",{},function(data){
                    console.log(data);
                    $scope.list = data.result;
                });
                alert(data.code+":"+data.msg);

            });
        }

        $scope.deleteRole = function(index){
            var obj = $scope.list[index];
            var data = { id:obj.id }
            angularHttpPost($http,"/api/acl_user/delete",data,function(data){
                console.log("data:",data);
                angularHttpPost($http,"/api/acl_user/getList",{},function(data){
                    console.log(data);
                    $scope.list = data.result;
                });
                alert(data.code+":"+data.msg);

            });
        }

//        提交权限树
        $scope.treeSubmit = function(){
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            var nodes = zTree.getCheckedNodes(true);
//            console.log(">> nodes:",nodes);
            var curUser = $scope.list[$scope.curIndex];
            var roleList = [];
            nodes.forEach(function(node){
                roleList.push( node.id );
            });
            var arg = {
                userId:curUser.id,
                roles:JSON.stringify(roleList)
            }
            console.log("arg",arg);
//            配置用户的权限
            angularHttpPost($http,"/api/acl_user/addUserRoles",arg,function(data){
                console.log("addUserRoles:",data);
                alert(data.code+":"+data.msg);
            });
        }


        var setting = {
            check: {
                enable: true,
                chkboxType: {"Y":"", "N":""}
            },
            view: {
                dblClickExpand: false
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback: {
                beforeClick: beforeClick,
            }
        };

        function beforeClick(treeId, treeNode) {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            zTree.checkNode(treeNode, !treeNode.checked, null, true);
            return false;
        }


    }
</SCRIPT>